package com.example.news;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.news.sql.MYSQL;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private Toolbar register_toolbar;
    private EditText edit_yonghumingaa;
    private EditText edit_mimaaa;
    private EditText edit_chongfumima;
    private CheckBox box_yuedu;
    private Button btn_zhuce;
    private TextView lijidenglu;
    MYSQL mysql;
    String reg = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        mysql =new MYSQL(RegisterActivity.this);

    }

    private void initView() {
        register_toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        setSupportActionBar(register_toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        register_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        edit_yonghumingaa = (EditText) findViewById(R.id.edit_yonghumingaa);
        edit_mimaaa = (EditText) findViewById(R.id.edit_mimaaa);
        edit_chongfumima = (EditText) findViewById(R.id.edit_chongfumima);
        box_yuedu = (CheckBox) findViewById(R.id.box_yuedu);
        btn_zhuce = (Button) findViewById(R.id.btn_zhuce);
        btn_zhuce.setOnClickListener(this);
        lijidenglu = (TextView) findViewById(R.id.lijidenglu);
        lijidenglu.setOnClickListener(this);
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_zhuce:
                String yonghumingaa = edit_yonghumingaa.getText().toString().trim();//获取输入的数据
                String mimaaa = edit_mimaaa.getText().toString().trim();
                String chongfumima = edit_chongfumima.getText().toString().trim();


                if (mimaaa.matches(reg)){//判断输入是否符合要求
                    if (mimaaa.equals(chongfumima)&&box_yuedu.isChecked()){//判断两次输入的密码是否相同且是否同意用于协议

                        JSONObject jsonObject=new JSONObject();
                        try {
                            jsonObject.put("name",yonghumingaa);
                            jsonObject.put("password",mimaaa);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        RequestQueue requestQueue= Volley.newRequestQueue(RegisterActivity.this);
                        JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, "http://192.168.0.107:8080/IDeaJ/WorldCupApp/user/regist", jsonObject.toString(), new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject jsonObject) {
                                Log.e("ATG", "onResponse: " );

                            }
                        }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError volleyError) {
                                Log.e("aaA", "onResponse: " );

                            }
                        });
                        requestQueue.add(jsonObjectRequest);



                        SQLiteDatabase sqLiteDatabase=mysql.getWritableDatabase();//数据库写入
                        sqLiteDatabase.execSQL("insert into information(name ,password)values(?,?)",new Object[]{yonghumingaa,mimaaa});
                        startActivity(new Intent(this,LoginAvtivity.class));//跳转到登陆界面
                        finish();//关闭页面
                    }else{
                        Toast.makeText(RegisterActivity.this,"两次密码不统一或未同意用户协议",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(RegisterActivity.this,"您输入的密码格式不正确,密码至少包含 数字和英文，长度6-20",Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.lijidenglu:
                startActivity(new Intent(RegisterActivity.this,LoginAvtivity.class));
                break;
        }
    }

    private void submit() {
        // validate
        String yonghumingaa = edit_yonghumingaa.getText().toString().trim();
        if (TextUtils.isEmpty(yonghumingaa)) {
            Toast.makeText(this, "用户名", Toast.LENGTH_SHORT).show();
            return;
        }

        String mimaaa = edit_mimaaa.getText().toString().trim();
        if (TextUtils.isEmpty(mimaaa)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String chongfumima = edit_chongfumima.getText().toString().trim();
        if (TextUtils.isEmpty(chongfumima)) {
            Toast.makeText(this, "重复密码", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
